+-2sigma.Rmdのロジックを良くしたい
やりたいこと
mm/ddのリターンが過去〇〇日のリターンの-2σ以上なのか以下なのか判別したい
rm(tmp)
rm(tmp1)
rm(tmp2)
rm(touraku2)
#過去1週間分を評価する
#結果テーブル準備
# tmp2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# tmp2 <- as.Date(tmp2[,1])
# touraku2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# touraku2[,1] <- as.Date(touraku2[,1])
tmp2 <- dailyrtn
tmp2 <- transform(tmp2, sigma=0)
tmp2 <- transform(tmp2, buysign='--')
tmp2[,1] <- ymd(tmp2[,1])
tmp2[,6] <- as.character(tmp2[,6])
tmp2 <- tmp2[-1,]
touraku2 <- head(dailyrtn,1)
touraku2 <- transform(touraku2, sigma=0)
touraku2 <- transform(touraku2, buysign='--')
touraku2[,1] <- ymd(touraku2[,1])
touraku2[,6] <- as.character(touraku2[,6])
touraku2 <- touraku2[-1,]
for (i in 1:length(ticker_tmp)){
tmp <- subset(dailyrtn, ticker == ticker_tmp[i])
#列追加
tmp <- transform(tmp, sigma=0)
tmp <- transform(tmp, buysign='--')
tmp$date <- ymd(tmp$date)
tmp[,6] <- as.character(tmp[,6])
tmp <- tmp[-1,]
#1週間前の位置 1週間前から現在までのcloseを評価するの意
tarRow1 <- nrow(tmp) - 6
for (k in tarRow1:nrow(tmp)) {
#過去300日分のリターンの-2σ計算
#tarRow2 <- k - 300
#過去300日分の抽出
#tmp1 <- subset(tmp,
# as.numeric(row.names(tmp)) >= tarRow2
# & row.names(tmp) <= tarRow1 )
tmp1 <- tmp[1:tarRow1,]
tmp1 <- tail(tmp1, 300)
#-2σ計算
under2sigma <- quantile(tmp1$rtn,c(0.02275))
#-2σの追記
tmp[k, 5] <- under2sigma
#リターンとunder2sigmaを比較してbuyサインを立てる
if (tmp[k, 4] < tmp[k, 5]) {
tmp[k, 6] <- 'buy'
} else {
tmp[k, 6] <- '-'
}
}
#結果をテーブルへ
tmp2 <- rbind(tmp2, tmp)
touraku2 <- subset(tmp2, buysign=='buy')
touraku2$ticker <- as.factor(touraku2$ticker)
#1週間以内でフィルタ
touraku2 <- touraku2 %>%
filter(date >= Sys.Date()-7)
}
datatable(touraku2,
filter = 'top',
style = 'bootstrap', class = 'table-bordered table-condensed',
extensions = 'ColReorder',
options = list(dom = 'Rlfrtip')
)
#write.table(touraku2,'touraku2.txt',sep="\t",quote=F,row.names = F)
ticker_bakusagari <- as.character(t(touraku2$ticker))
#購入済で下がった銘柄は?
ticker_kaimashikoho <- intersect(ticker_bakusagari, kounyu)
#購入済でない銘柄は?
#setdiff(a,b)はaにあってbにないものを抽出する
ticker_shinkikoho <- setdiff(ticker_bakusagari, kounyu)
tmp <- subset(kabu.com_list, kabu.com_list$ticker %in% ticker_bakusagari)
datatable(tmp,
filter = 'top',
style = 'bootstrap', class = 'table-bordered table-condensed',
extensions = 'ColReorder',
options = list(dom = 'Rlfrtip')
)
購入済で下がった銘柄新規購入候補の銘柄
1471, 1327, 2066, 3462
#国内
if (length(intersect(touraku2$ticker, kokunaiETF)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
#海外
if (length(intersect(touraku2$ticker, kaigai)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
ggplotly(
ggplot(subset(monthlyrtn, ticker %in% ticker_bakusagari),
aes(x = return_monthly, colour = ticker)
) + geom_density()
)
# 購入済銘柄との相関確認
ticker4 <- c()
f02 <- daily1 %>%
filter(ticker %in% ticker_bakusagari | ticker %in% kounyu,
date >= Sys.Date() - 365)
f02 <- dcast(f02, date ~ ticker, value.var='close')
f02 <- f02[,2:ncol(f02)]
f02 <- f02[,order(factor(colnames(f02),levels=ticker4))]
cor.plot(cor(f02, use='pairwise.complete.obs', method='p'),
numbers=T)
corrplot.mixed(cor(f02, use='pairwise.complete.obs', method='p'),
order = "hclust", tl.col = "black")
#qgraph
f03 <- f02
groups <- list("購入済"=1:length(kounyu),"新規"=length(kounyu)+1:length(ticker_shinkikoho))
qgraph(cor(f03, use='pairwise.complete.obs', method='p'),groups=groups)
$(function(){
$("img:not(.lb-image)").wrap(function() {
return "<a href='" + $(this).attr("src") + "' data-lightbox='" + $(this).attr("scr") + "'></a>";
});
});